package bandidong.bean;

import bandidong.dao.CompanyDAO;
import bandidong.dao.impl.CompanyDAOImpl;
import bandidong.model.Company;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
public class CompanyBean implements CompanyBeanRemote {
    
    @PersistenceContext(unitName = "persistenceUnit")
    EntityManager em;
    
    private CompanyDAO companyDAO = null;
    
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Company> getAllCompanies() {
        companyDAO = new CompanyDAOImpl(em);
        return companyDAO.getAllCompanies();
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Company getCompanyById(int companyId) {
        companyDAO = new CompanyDAOImpl(em);
        return companyDAO.getCompanyById(companyId);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Company> getAllCompanies(int page, int pageSize) {
        companyDAO = new CompanyDAOImpl(em);
        return companyDAO.getAllCompanies(page, pageSize);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean checkExists(String companyName, int companyId) {
        companyDAO = new CompanyDAOImpl(em);
        return companyDAO.checkExists(companyName, companyId);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String addCompany(Company company) {
        companyDAO = new CompanyDAOImpl(em);
        return companyDAO.addCompany(company);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String updateCompany(Company company) {
        companyDAO = new CompanyDAOImpl(em);
        return companyDAO.updateCompany(company);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String deleteCompany(Company company) {
        companyDAO = new CompanyDAOImpl(em);
        return companyDAO.deleteCompany(company);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Company getCompanyByName(String companyName) {
        companyDAO = new CompanyDAOImpl(em);
        return companyDAO.getCompanyByName(companyName);
    }
}
